package com.lhyl.front.web.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.lhyl.front.common.AppPropertiesUtil;
import com.lhyl.front.common.StringUtils;
import com.lhyl.front.constant.FrontConstant;
import com.lhyl.front.constant.PayConstant;
import com.lhyl.front.web.controller.BaseController;
import com.lhyl.front.web.shiro.model.User;
import com.lhyl.front.web.user.model.UserCharge;
import com.lhyl.front.web.user.service.UserChargeService;

/***
 * 
 * @author huang
 *
 */
@Controller
public class UserChargeController extends BaseController
{
	@Autowired
    private UserChargeService userChargeService;
      
	@RequestMapping(value="/content/charge/onlineCharge",method={RequestMethod.POST,RequestMethod.GET})
	public void onlineCharge(HttpServletRequest req,HttpServletResponse repson,ModelMap map)throws Exception
	{
		 User user=super.getLoginUserObject();
		 String bankType = req.getParameter("bank_type"); //获取银行类型
		 String payNo = req.getParameter("payno");//支付的订单号
		 String fee =req.getParameter("fee");
		 StringBuilder sb  = new StringBuilder();
		 sb.append(AppPropertiesUtil.getStringValue(PayConstant.PAY_URL)).append("?");
		 sb.append("fee=").append(fee).append("&");
		 sb.append("bank_type=").append(bankType).append("&");
		 sb.append("payno=").append(payNo).append("&");
		 sb.append("uid=").append(user.getUid()).append("&");
		 sb.append("username=").append(user.getLoginName()).append("&");
		 sb.append("sign=").append(AppPropertiesUtil.getStringValue(PayConstant.EXTERNAL_KEY)).append("&");
		 sb.append("callback=").append(AppPropertiesUtil.getStringValue(PayConstant.PAY_CALLBACK));
		 this.renderText(repson, sb.toString());
	}
	
	
	@RequestMapping(value="/content/charge/chargeSuccess")
	public void chargeSuccess(HttpServletRequest req,HttpServletResponse repson,ModelMap map)throws Exception
	{
		 String chargeOrder = req.getParameter("chargeOrder"); //获取支付订单
		 UserCharge charge  = userChargeService.queryUserCharge(chargeOrder);
		 if(charge!=null)//该订单已经删除了 
		 {
			 if(charge.getIsDelete()==PayConstant.CHARGE_NORMAL)
			 {			 
			     if(StringUtils.equals(charge.getStatus(),PayConstant.CHARGE_IS_NOTHANDLE))//未支付的订单
			     { 
				     userChargeService.charge(charge);
			     }
			     else
				 {
					 this.renderMsg(repson, FrontConstant.FAILED_CODE, "充值订单【"+chargeOrder+"】已经处理。", StringUtils.EMPTY);
				 }
			 }
			 else
			 {
				 this.renderMsg(repson, FrontConstant.FAILED_CODE, "充值订单【"+chargeOrder+"】已经删除。", StringUtils.EMPTY);
			 }
		 }
		 else
		 {
			 this.renderMsg(repson, FrontConstant.FAILED_CODE, "充值订单【"+chargeOrder+"】错误。", StringUtils.EMPTY);
		 }
	}
	
}
